<template>
  <div class="app-container">
    <el-form style="margin-top: 20px;" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病历号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入病历号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人编号" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入病人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生编号" prop="doctorId">
        <el-input
          v-model="queryParams.doctorId"
          placeholder="请输入医生编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会诊日期" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择会诊日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="病历主键" align="center" prop="id" />
      <el-table-column label="病历号" align="center" prop="number" />
      <el-table-column label="病人编号" align="center" prop="patientId" />
      <el-table-column label="医生编号" align="center" prop="doctorId" />
      <el-table-column label="主述" align="center" prop="priDesc" />
      <el-table-column label="现有病史" align="center" prop="presentIllness" />
      <el-table-column label="现病史治疗情况" align="center" prop="presentMedicalCondition" />
      <el-table-column label="既往病史" align="center" prop="pmh" />
      <el-table-column label="过敏史" align="center" prop="allergy" />
      <el-table-column label="体格检查" align="center" prop="healthCheckup" />
      <el-table-column label="检查建议" align="center" prop="checkAdvice" />
      <el-table-column label="注意事项" align="center" prop="attention" />
      <el-table-column label="会诊日期" align="center" prop="date" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病历对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="病历号" prop="number">
          <el-input v-model="form.number" placeholder="请输入病历号" />
        </el-form-item>
        <el-form-item label="病人编号" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入病人编号" />
        </el-form-item>
        <el-form-item label="医生编号" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入医生编号" />
        </el-form-item>
        <el-form-item label="主述" prop="priDesc">
          <el-input v-model="form.priDesc" placeholder="请输入主述" />
        </el-form-item>
        <el-form-item label="现有病史" prop="presentIllness">
          <el-input v-model="form.presentIllness" placeholder="请输入现有病史" />
        </el-form-item>
        <el-form-item label="现病史治疗情况" prop="presentMedicalCondition">
          <el-input v-model="form.presentMedicalCondition" placeholder="请输入现病史治疗情况" />
        </el-form-item>
        <el-form-item label="既往病史" prop="pmh">
          <el-input v-model="form.pmh" placeholder="请输入既往病史" />
        </el-form-item>
        <el-form-item label="过敏史" prop="allergy">
          <el-input v-model="form.allergy" placeholder="请输入过敏史" />
        </el-form-item>
        <el-form-item label="体格检查" prop="healthCheckup">
          <el-input v-model="form.healthCheckup" placeholder="请输入体格检查" />
        </el-form-item>
        <el-form-item label="检查建议" prop="checkAdvice">
          <el-input v-model="form.checkAdvice" placeholder="请输入检查建议" />
        </el-form-item>
        <el-form-item label="注意事项" prop="attention">
          <el-input v-model="form.attention" placeholder="请输入注意事项" />
        </el-form-item>
        <el-form-item label="会诊日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择会诊日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/emr/record";
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/emr/patient";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病历表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示病人框
      selectPatient: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        patientId: null,
        doctorId: null,
        priDesc: null,
        presentIllness: null,
        presentMedicalCondition: null,
        pmh: null,
        allergy: null,
        healthCheckup: null,
        checkAdvice: null,
        attention: null,
        date: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询病历列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        patientId: null,
        doctorId: null,
        priDesc: null,
        presentIllness: null,
        presentMedicalCondition: null,
        pmh: null,
        allergy: null,
        healthCheckup: null,
        checkAdvice: null,
        attention: null,
        date: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 选择患者信息按钮 */
    handleSelect(){
      this.selectPatient = true;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加病历";
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除病历编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

  }
};
</script>
